<template>
  <div class="navigation">
    <ul>
        <template v-for="(item,index) in props.menu">
            <li class="list" :class="{ active: tabs.currentTab == index }" @click="tabs.currentTab = index" :style="{'--clr': item.iconColor}">
                <div @click="router.push(item.url)">
                    <span class="icon"><i :class="item.iconfont"></i></span>
                    <span class="text">{{item.text}}</span>
                </div>
            </li>
        </template>
      <div class="indicator"></div>
    </ul>
  </div>
</template>

<script setup>
import router from "@/router";
import { reactive, watchEffect } from "vue";
import {onMounted} from "@vue/runtime-core";
const props = defineProps({
  menu: {
    type: Array,
    default: [
        { url: '/home', iconfont: 'home-outline', iconColor: '#f44336', text: '主页'},
        { url: '/tags', iconfont: 'person-outline', iconColor: '#ffa11f', text: '标签'},
        { url: '/marks', iconfont: 'chatbubble-outline', iconColor: '#0fc70f', text: '标记'},
        { url: '/settings', iconfont: 'camera-outline', iconColor: '#2196f3', text: '设置'}
    ],
  }
});

const tabs = reactive({
    currentTab: 0,
})

const emit = defineEmits(["handleChangeCurrentTab"]);

onMounted(() =>{
    /* 直接在tabbar组件页面监听，判断当前页面是哪个页面,
    解决刷新时,tab默认为 0 的bug */
    const navBarActive = window.location.hash
    if (navBarActive.includes('#/home')) {
      tabs.currentTab = 0
    } else if (navBarActive.includes('#/labels')) {
      tabs.currentTab = 1
    } else if (navBarActive.includes('#/marks')) {
      tabs.currentTab = 2
    } else if (navBarActive.includes('#/settings')) {
      tabs.currentTab = 3
    }
})


const stopWatch = watchEffect(() => {
  // 向父组件传参
  emit("handleChangeCurrentTab", tabs.currentTab);
});


</script>

<style lang="less" scoped>
.navigation{
    box-sizing: border-box;
    font-family: 'Poppins', sans-serif;
    width: 100%;
    height: 60px;
    background-color: #fff;
    display: flex;
    justify-content: center;
    align-items: center;
    border-top-left-radius: 10px;
    border-top-right-radius: 10px;
    position: fixed;
    bottom: -1px;
    left: 0;
}
.navigation ul{
    display: flex;
}
.navigation ul li{
    position: relative;
    list-style: none;
    width: 70px; /* 350/5 = 70 */
    height: 60px;
    z-index: 2;
}
.navigation ul li div{
    position: relative;
    display: flex;
    justify-content: center;
    align-items: center;
    text-align: center;
    flex-direction: column;
    width: 100%;
    height: 100%;
    font-weight: 500;
}
.navigation ul li div .icon{
    position:relative;
    display: block;
    width: 55px;
    height: 55px;
    line-height: 65px;
    border-radius: 50%;
    -webkit-border-radius: 50%;
    -moz-border-radius: 50%;
    -ms-border-radius: 50%;
    -o-border-radius: 50%;
    font-size: 1.5em;
    text-align: center;
    transition: 0.5s;
    -webkit-transition: 0.5s;
    -moz-transition: 0.5s;
    -ms-transition: 0.5s;
    -o-transition: 0.5s;
    color: var(--clbg);
}
.navigation ul li.active div .icon{
    background-color: var(--clr);
    color: #fff;
    transform: translateY(-27px);
    -webkit-transform: translateY(-27px);
    -moz-transform: translateY(-27px);
    -ms-transform: translateY(-27px);
    -o-transform: translateY(-27px);
}
.navigation ul li div .icon i{
    font-size: 20px;
}
.navigation ul li.active div .icon i{
    font-size: 26px;
    position: absolute;
    left: 14px;
    top: -3px;
}
.navigation ul li div .icon::before{
    content: '';
    position: absolute;
    top: 10px;
    left: 0;
    width: 100%;
    height: 100%;
    background-color: var(--clr);
    border-radius: 50%;
    -webkit-border-radius: 50%;
    -moz-border-radius: 50%;
    -ms-border-radius: 50%;
    -o-border-radius: 50%;
    filter: blur(5px);
    -webkit-filter: blur(5px);
    opacity: 0;
}
.navigation ul li.active div .icon::before{
    opacity: 0.5;
}
.navigation ul li div .text{
    position: absolute;
    color: var(--clbg);
    font-size: 0.75em;
    font-weight: var(--primary-font-weight);
    font-family: var(--primary-font-family);
    letter-spacing: 0.05em;
    transition: 0.5s;
    -webkit-transition: 0.5s;
    -moz-transition: 0.5s;
    -ms-transition: 0.5s;
    -o-transition: 0.5s;
    opacity: 0;
    transform: translateY(20px);
    -webkit-transform: translateY(20px);
    -moz-transform: translateY(20px);
    -ms-transform: translateY(20px);
    -o-transform: translateY(20px);
}
.navigation ul li.active div .text{
    opacity: 1;
}
.indicator{
    position: absolute;
    top: -35px;
    width: 70px;
    height: 70px;
    background: #fff;
    border-radius: 50%;
    -webkit-border-radius: 50%;
    -moz-border-radius: 50%;
    -ms-border-radius: 50%;
    -o-border-radius: 50%;
    transition: 0.5s;
    -webkit-transition: 0.5s;
    -moz-transition: 0.5s;
    -ms-transition: 0.5s;
    -o-transition: 0.5s;
    z-index: 1;
}
.indicator::before{
   content: '';
   position: absolute;
   top: 5px;
   left: -28px;
   width: 30px;
   height: 30px;
   background-color: transparent;
   border-radius: 50%;
   -webkit-border-radius: 50%;
   -moz-border-radius: 50%;
   -ms-border-radius: 50%;
   -o-border-radius: 50%;
   box-shadow: 15px 18px #fff;

}
.indicator::after{
    content: '';
    position: absolute;
    top: 5px;
    right: -28px;
    width: 30px;
    height: 30px;
    background-color: transparent;
    border-radius: 50%;
    -webkit-border-radius: 50%;
    -moz-border-radius: 50%;
    -ms-border-radius: 50%;
    -o-border-radius: 50%;
    box-shadow: -15px 18px #fff;
}
.navigation ul li:nth-child(1).active ~ .indicator{
    transform: translateX(0);
    -webkit-transform: translateX(0);
    -moz-transform: translateX(0);
    -ms-transform: translateX(0);
    -o-transform: translateX(0);
}
.navigation ul li:nth-child(2).active ~ .indicator{
    transform: translateX(70px);
    -webkit-transform: translateX(70px);
    -moz-transform: translateX(70px);
    -ms-transform: translateX(70px);
    -o-transform: translateX(70px);
}
.navigation ul li:nth-child(3).active ~ .indicator{
    transform: translateX(140px);
    -webkit-transform: translateX(140px);
    -moz-transform: translateX(140px);
    -ms-transform: translateX(140px);
    -o-transform: translateX(140px);
}
.navigation ul li:nth-child(4).active ~ .indicator{
    transform: translateX(210px);
    -webkit-transform: translateX(210px);
    -moz-transform: translateX(210px);
    -ms-transform: translateX(210px);
    -o-transform: translateX(210px);
}
.navigation ul li:nth-child(5).active ~ .indicator{
    transform: translateX(280px);
    -webkit-transform: translateX(280px);
    -moz-transform: translateX(280px);
    -ms-transform: translateX(280px);
    -o-transform: translateX(280px);
}
</style>
